#include "../io/options.h"

static int on_option_v( const char *value, void *args )
{
	int *verbose = args;
	fprintf( stderr, "-- parsing verbose='%s'\n", value );
	*verbose = strtol(value, NULL, 10);
	return 0;
}

static int on_option_f( const char *filename, void *args )
{
	mds_options *options = args;
	fprintf( stderr, "-- appending options from file '%s'\n", filename );
	if( mds_options_load( options, filename, "f") < 0 )
	{
		mds_perror( stderr, "mds_options_load(%s)", filename );
		return -1;
	}
	return 0;
}

static int on_option_example( const char *value, void *args )
{
	const char **example = args;
	fprintf( stderr, "-- parsing example='%s'\n", value );
	*example = value;
	return 0;
}

#define ERROR prog_exit = -1; goto END 

int main( int argc, char *argv[] )
{
	mds_options options   = MDS_OPTIONS_INIT;
	int         verbose   = 0;
	int         prog_exit = 0;
	const char *example   = NULL;
	
	MDS_OPTIONS_DECL()
	{
		MDS_OPTION_NORMAL(f,options, "load options from file" ),
		MDS_OPTION_NORMAL(v,verbose, "set verbose (default=0)"),
		MDS_OPTION_FORCED(example,example, "an example string")
	}
	MDS_OPTIONS_DONE();
	
	if( argc <= 1 )
	{
		MDS_OPTIONS_HELP(stderr);
		return 1;
	}
	
	if( mds_options_parse( &options, argc, argv) < 0 )
	{
		mds_perror( stderr, "mds_parse_options" );
		ERROR;
	}
	
	MDS_MEMORY();
	MDS_OPTIONS_DISPATCH(options,ERROR);
	MDS_MEMORY();
	
END:
	MDS_MEMORY();
	mds_options_quit( &options );
	MDS_MEMORY();
	return prog_exit;
}
